﻿using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Janus.Mes.IDal;
using Janus.BaseEntitys;
using Janus.Cores.Extension;

namespace Janus.Mes.Services
{
    /// <summary>
    /// 基础服务类
    /// </summary>
    /// <typeparam name="DTO">BaseDTO数据传输对象</typeparam>
    /// <typeparam name="Idal">IBaseDal数据访问仓储</typeparam>
    /// <typeparam name="POCO">数据库映射对象 </typeparam>
    public class BaseServices<DTO, Idal, POCO> : IBase<DTO>
        where DTO : BaseDTO, new()
        where Idal : IBaseDal<POCO, DTO>
        where POCO : Entity, new()
    {
        /// <summary>
        /// 数据访问接口
        /// </summary>
        protected Idal dal = ServiceFactory.LazyT<Idal>();

        public bool Add(List<DTO> ts)
        {
            return dal.Add<DTO, POCO>(ts);
        }

        public bool Add(DTO t)
        {
            return dal.Add<DTO, POCO>(t);
        }

        public bool BatchInset(List<DTO> models)
        {
            return dal.BatchInset<DTO, POCO>(models);
        }

        public bool BatchSave(List<DTO> models)
        {
            return dal.BatchSave<DTO, POCO>(models);
        }

        public bool BatchUpdate(List<DTO> models)
        {
            return dal.BatchUpdate<DTO, POCO>(models);
        }

        public bool Delete(Conditioner conditions)
        {
            return dal.Delete(conditions);
        }

        public bool Delete(string predicate)
        {
            return dal.Delete(predicate);
        }

        public bool Delete(Querier predicate)
        {
            return dal.Delete<POCO>(predicate);
        }

        public bool Delete(params object[] entity)
        {
            return dal.Delete(entity);
        }

        public IList<DTO> FindAll(Querier predicate)
        {
            return dal.FindAll<POCO, DTO>(predicate);
        }

        public ResponseResult<IList<DTO>> FindAll(SearchEntity<DTO> searchEntity)
        {
            var searchEntityPOCO = searchEntity.ToPOCO<DTO, POCO>();//  AutoMapper.Mapper.DynamicMap<SearchEntity<POCO>>(searchEntity);
            var result = dal.FindAll<POCO, DTO>(searchEntityPOCO);
            return result;
        }

        public IList<DTO> FindAll(string predicate, string orderBy = null)
        {
            return dal.FindAll<POCO, DTO>(predicate, orderBy);
        }

        public ResponseResult<IList<DTO>> FindAllByPro(PageCriteria pageCriteria)
        {
            return dal.FindAllByPro<POCO, DTO>(pageCriteria);
        }

        public DTO FindOne(string predicate)
        {
            return dal.FindOne<POCO, DTO>(predicate);
        }

        public DTO FindOne(Querier predicate)
        {
            return dal.FindOne<POCO, DTO>(predicate);
        }

        public DTO FindOne(params object[] entity)
        {
            return dal.FindOne<POCO, DTO>(entity);
        }

        public object FindScalar(string field, Querier predicate)
        {
            return dal.FindScalar<POCO>(field, predicate);
        }

        public object FindScalar(string field, string predicate)
        {
            return dal.FindScalar<POCO>(field, predicate);
        }

        public bool IsExists(Querier predicate)
        {
            return dal.IsExists<POCO>(predicate);
        }

        public bool IsExists(string predicate)
        {
            return dal.IsExists(predicate);
        }

        public bool Save(List<DTO> entitys)
        {
            return dal.Save<DTO, POCO>(entitys);
        }

        public int Save(DTO t)
        {
            return dal.Save<DTO, POCO>(t);
        }

        public bool Update(List<DTO> entitys)
        {
            return dal.Update<DTO, POCO>(entitys);
        }

        public int Update(DTO t)
        {
            return dal.Update<DTO, POCO>(t);
        }

        public bool UpdateIsEnable(Querier queriers)
        {
            return dal.UpdateIsEnable<POCO>(queriers);
        }

        public int UpdateSetion(DTO entity, Querier conditions)
        {
            return dal.UpdateSetion<DTO, POCO>(entity, conditions);
        }

        public int UpdateSetion(Hashtable updater, Querier predicate)
        {
            return dal.UpdateSetion<POCO>(updater, predicate);
        }

        public int UpdateSetion(Hashtable updater, string predicate)
        {
            return dal.UpdateSetion(updater, predicate);
        }

        public int UpdateSetion(DTO entity, Conditioner conditions)
        {
            return dal.UpdateSetion<DTO, POCO>(entity, conditions);
        }

        //public int UpdateSetion(DynamicEntity entity, Conditioner conditions)
        //{
        //    return dal.UpdateSetion(entity, conditions);
        //}
    }
}
